import {Component} from '@angular/core';
import {AlertController, IonicPage, ModalController, NavController, NavParams, ViewController} from 'ionic-angular';
import {ApplySucceedConfirmPage} from "../apply-succeed-confirm/apply-succeed-confirm";
import {APARTMENTAPISERVICE, BuildingModel} from "../../api/apartment-api.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {USER} from "../../service/user.service";

/**
 * Generated class for the ExchangeRoomConfirmPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-exchange-room-confirm',
  templateUrl: 'exchange-room-confirm.html',
})
export class ExchangeRoomConfirmPage {
  data = {
    newHouseNo: null,
    newApartmentId: 0
  };
  apartmentList:string[];
  apartmentId:number;
  buildingData ?:BuildingModel;
  ContractId:number


  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              private viewCtrl:ViewController,
              private modalCtrl:ModalController,
              protected alertCtrl:AlertController,
              private USER:USER,
              private ls:LocalStorage,
              private apartmentApiService:APARTMENTAPISERVICE) {
    this.ContractId = this.ls.getObject('contractId');
  }

  ionViewDidLoad() {
    if (!this.apartmentList) {
      this.apartmentApiService.getList((v => {
        this.apartmentList = v;
        this.data.newApartmentId = v[0].id;
      }));
    }
  }

  get buildingList() {
    return this.buildingData ? this.buildingData.buildingVOs : [];
  }

  dismiss() {
    console.log("dismiss");
    this.viewCtrl.dismiss();
  }

  prevent(e) {
    e.stopPropagation();
  }

  apply(e) {
    console.log(this.data.newHouseNo);
    if (!this.data.newApartmentId) {
      this.USER.toast("请选择公寓")
      return
    }else if(!this.data.newHouseNo){
      this.USER.toast("请填写房间号")
      return
    }
    this.apartmentApiService.exchangeRoom(this.ContractId, {
      newApartmentId: this.data.newApartmentId,
      newHouseNo: this.data.newHouseNo,
      operation: 3
    }, v=> {
      this.doCheckout()
    })
  }

  doCheckout() {
    this.dismiss();
    let profileModal = this.modalCtrl.create(ApplySucceedConfirmPage, this.data, {showBackdrop: true});
    profileModal.present();
  }
}
